package com.zhiwei.gateway.service.impl;

import com.zhiwei.gateway.dao.entity.UserInfo;
import com.zhiwei.gateway.dao.entity.UserInfoExample;
import com.zhiwei.gateway.dao.mapper.UserInfoMapper;
import com.zhiwei.gateway.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * function: 用户服务(注册、删除、token用户服务)
 * author: zhiwei_yang
 * time: 2020/2/12-15:53
 */
@Slf4j
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo getDefaultUserInfo() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("common");
        userInfo.setPassword("common");
        return userInfo;
    }

    @Override
    public UserInfo getUserInfoByName(String username) {
        if(StringUtils.isEmpty(username)){
            return null;
        }
        UserInfoExample userInfoExample = new UserInfoExample();
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<UserInfo> userInfoList = userInfoMapper.selectByExample(userInfoExample);
        log.info("查询用户信息username:[{}], userInfo ==> {}", username, userInfoList);
        return CollectionUtils.isEmpty(userInfoList) ? null : userInfoList.get(0);
    }

    @Override
    public boolean addUser(UserInfo userInfo) {
        return userInfoMapper.insert(userInfo) == 1;
    }
}
